package team

import (
	cs_msg "sanguo/protocol/cs/message"

	"github.com/golang/protobuf/proto"
)

func UpdateTeamToClients(teamObj *Team) {
	_team := &cs_msg.Team{
		TeamID:   proto.Uint64(teamObj.TeamID),
		TeamType: proto.Int32(teamObj.TeamType),
		Header:   proto.Uint64(teamObj.Header),
	}
	for _, r := range teamObj.GetRoles() {
		_role := &cs_msg.ViewObj{
			RoleID:     proto.Uint64(r.RoleID),
			UserID:     proto.String(r.UserID),
			Level:      proto.Int32(r.Level),
			Name:       proto.String(r.Name),
			Portrait:   proto.Int32(r.Portrait),
			Outline:    proto.Int32(r.Outline),
			Face:       proto.Int32(r.Face),
			Hair:       proto.Int32(r.Hair),
			HairColor:  proto.Int32(r.HairColor),
			EyeColor:   proto.Int32(r.EyeColor),
			Cloth:      proto.Int32(r.Cloth),
			ClothColor: proto.Int32(r.ClothColor),
			Weapon1:    proto.Int32(r.Weapon1),
		}
		_team.Roles = append(_team.Roles, _role)
		_team.RoleStatus = append(_team.RoleStatus, r.Status)
	}

	resp := &cs_msg.UpdateTeamToC{
		UpdateTeam: _team,
	}
	for _, r := range teamObj.GetRoles() {
		r.SendToClient(resp)
	}
}

func UpdateFightInvite(teamObj *Team) {
	fightObj := teamObj.Fight
	if fightObj == nil {
		return
	}

	resp := &cs_msg.UpdateFightInviteToC{
		BattleLevelType: proto.Int32(fightObj.BattleLevelType),
	}

	for roleID, reply := range fightObj.RoleReply {
		_resp := &cs_msg.FightInviteRoleReply{
			RoleID: proto.Uint64(roleID),
		}
		if reply == 0 {
			_resp.Status = cs_msg.FightInviteType(cs_msg.FightInviteType_ToDo).Enum()
		} else if reply == 1 {
			_resp.Status = cs_msg.FightInviteType(cs_msg.FightInviteType_Agree).Enum()
		} else if reply == 2 {
			_resp.Status = cs_msg.FightInviteType(cs_msg.FightInviteType_DisAgree).Enum()
		}

		resp.RoleReplys = append(resp.RoleReplys, _resp)
	}

	for _, r := range teamObj.GetRoles() {
		r.SendToClient(resp)
	}

}
